﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PropertyManagementSystemAPI.Models;

namespace PropertyManagementSystemAPI.Controllers
{
    /// <summary>
    /// 报修管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class RepairInfoController : ControllerBase
    {
        private readonly IFreeSql db;

        public RepairInfoController(IFreeSql _db)
        {
            this.db = _db;
        }

        /// <summary>
        /// 获取报修信息列表
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="limit">页条数</param>
        /// <param name="strName">报修描述</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getRepairInfo(int page, int limit, string? strName)
        {
            var list = db.Select<Repairinfo, Ownerinfo>()
                 .LeftJoin((a, b) => a.Oid == b.Oid)
                 .Count(out var total)
                 .Page(page, limit)
                 .Where((a, b) => ((string.IsNullOrEmpty(strName)) || (a.Repairremark.Contains(strName))) && a.Isdelete == false)
                 .ToList((a, b) => new { a.Repairid, a.Oid, a.Equipmentname, a.Repairremark, a.Createtime, a.Repairstate, a.Processor, a.Processphone, b.Orealname });

            var json = new
            {
                total = total,
                data = list,
                code = 200
            };

            return Ok(json);
        }

        /// <summary>
        /// 添加报修信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult addInfo([FromBody] Repairinfo info)
        {
            try
            {
                info.Createtime = DateTime.Now;
                info.Isdelete = false;
                int res = db.Insert(info).ExecuteAffrows();
                return Ok(res > 0);
            }
            catch (Exception)
            {
                return Ok(false);
            }
        }
        /// <summary>
        /// 编辑报修信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult editInfo([FromBody] Repairinfo info)
        {
            try
            {
                info.Isdelete = false;
                int res = db.Update<Repairinfo>()
                    .SetSource(info)
                    .ExecuteAffrows();
                return Ok(res > 0);
            }
            catch (Exception)
            {
                return Ok(false);
            }
        }

        /// <summary>
        /// 删除报修信息
        /// </summary>
        /// <param name="rid">报修id</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult deleteInfo(int rid)
        {
            try
            {
                int res = db.Update<Repairinfo>(rid)
                    .Set(a => a.Isdelete == true)
                    .ExecuteAffrows();
                return Ok(res > 0);
            }
            catch (Exception)
            {
                return Ok(false);
            }
        }
    }
}
